/** 
 * @CopyRight (c) 2012-2012
 * @Project <eshop>
 * @JDK_Version_Used:<JDK1.6>
 * @Description:TODO<描述用途>
 * @Title: RemoveWeb.java
 * @Author: <何俊>
 * @Author_of_Contact <E:azj1001@sina.com T:18651831939>
 * @Date 2012-12-8 下午10:29:49
 * @Modified_By: <修改人>
 * @Modified_of_Contact <修改人联系方式>
 * @Modified_Date <修改日期时间>
 * @Why & What is modified <修改原因描述>
 * @Version v1.00
 */ 
package com.e.buy.web.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.commons.query.ConditionBean;

import com.e.buy.BuyTools;
import com.e.buy.bean.back.manager.CmiAdminUser;
import com.e.buy.enums.Commons;
import com.e.buy.enums.Status;
import com.e.buy.service.CommonsServiceClient;

/**
 * @Description:TODO(描述类别作用)
 * @author <何俊>
 * @date 2012-12-8 下午10:29:49
 * @version v1.0
 * @Other :@see #Files
 */
@Controller
@RequestMapping("/impl_remove")
public class AdminRemoveWeb extends BuyTools{

	@Resource
	private CommonsServiceClient commons = null;
	
	public void setCommons(CommonsServiceClient commons) {
		this.commons = commons;
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/backRemove.adm")
	public String commonsRemove(@RequestParam("data") String data, @RequestParam("st") String st,
			@RequestParam("msg") String msg,HttpSession session) {
		String rtMsg = "";
		int errorCode = 0;
		String sql = null;
		boolean log = true;
		String doWhatMsg = "";
		boolean hasDone = false;
		Object[] args = new Object[0];
		JSONObject success = new JSONObject();
		List<ConditionBean> beans = new ArrayList<ConditionBean>();
		CmiAdminUser user = ((CmiAdminUser) session.getAttribute("loginAdmin")).clone();
		Status status = Status.REMOVE;
		
		try{
			Commons.BackProcess e = Commons.getBackProcess(st);
			rtMsg = this.getWebProperties("web", "msg.success");
			doWhatMsg = this.getWebProperties("web", e.getStatus() + ".REMOVE.log") + "["+msg+"]";
			sql = commons.getEnumSql(e, status);
			switch (e) {
			case ADMIN_POWER_CMI_ROLE:
				hasDone = true;
				//删除角色
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("uuid", "customer", " uuid IN ("+data+")", "0", null));
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, args);
				
				//删除角色权限
				try{
					beans = new ArrayList<ConditionBean>();
					beans.add(new ConditionBean("roleid", "customer", "cmi_role_power.roleid IN ("+data+")", "0", null));
					sql = commons.getEnumSql(Commons.BackProcess.ADMIN_POWER_CMI_ROLE_POWER, status);
					service.executeSQL(sql, beans, args);
				} catch (Exception e2) {
					// 删除权限失败，忽略
				}
				break;
			case ADMIN_INFO:
				hasDone = true;
				//删除管理员信息
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("uuid", "customer", " uuid IN ("+data+")", "0", null));
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, args);
				
				//删除管理员权限
				try{ 
					beans = new ArrayList<ConditionBean>();
					beans.add(new ConditionBean("adminid", "customer", "adminid IN ("+data+")", "0", null));
					sql = commons.getEnumSql(Commons.BackProcess.ADMIN_INFO_POWER, status);
					service.executeSQL(sql, beans, args);
				} catch (Exception e2) {
					// 删除权限失败，忽略
				}
				break;
			case GOODS_CATEGROY:
				hasDone = true;
				//删除商品类型
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("uuid", "customer", " uuid IN ("+data+")", "0", null));
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, args);
				
				//删除 商品类型下的所有属性
				try {
					beans = new ArrayList<ConditionBean>();
					beans.add(new ConditionBean("categroyId", "customer", " categroyId IN ("+data+")", "0", null));
					sql = commons.getEnumSql(Commons.BackProcess.GOODS_CATEGROY_PROPERTIES, status);
					service.executeSQL(sql, beans, args);
				} catch (Exception e2) {
					// 删除失败，忽略
				}
				break;
			case ADMIN_LOG:
			case GOODS_BRAND:
			case GOODS_CATEGROY_PROPERTIES:
			case NEWS:
			case SINGLE:
			case AD:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(data, ConditionBean.class);
				break;
			
			case GOODS_VARIETY:
				hasDone = true;
				String vct = "0";
				
				//验证是否是叶子分类
				sql = service.getSql("sql", "cmi_goods.variety_list.remove.check.leaf");
				vct = (String) service.queryForObject(sql, String.class, data);
				if(!vct.equals("0")){
					rtMsg = this.getWebProperties("web", "GOODS_VARIETY.REMOVE.check.leaf");
					break;
				}
				//TODO 验证是否存在商品
				sql = service.getSql("sql", "cmi_goods.variety_list.remove.check.goods");
//				vct = (String) service.queryForObject(sql, String.class, data);
				if(!vct.equals("0")){
					rtMsg = this.getWebProperties("web", "GOODS_VARIETY.REMOVE.check.goods");
					break;
				}
				
				//执行删除
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, data);
				break;
			case NEWS_TYPE:
				hasDone = true;
				vct = "0";

				//TODO 验证是否存在新闻
				sql = service.getSql("sql", "cmi_news.news_type_list.remove.check.news");
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("typeId", "customer", " typeId IN ("+data+")", "0", null));
				
				vct = (String) service.queryForObject(sql, String.class,  beans, args);
				if(!"0".equals(vct) && vct != null){
					rtMsg = this.getWebProperties("web", "cmi_news_type.remove.check.news");
					break;
				}
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("uuid", "customer", " uuid IN ("+data+")", "0", null));
				
				//执行删除
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, args);
				break;
			case SINGLE_TYPE:
				hasDone = true;
				vct = "0";

				//TODO 验证是否存在单页
				sql = service.getSql("sql", "cmi_single.single_type_list.remove.check.single");
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("typeId", "customer", " typeId IN ("+data+")", "0", null));
				
				vct = (String) service.queryForObject(sql, String.class,  beans, args);
				if(!"0".equals(vct) && vct != null){
					rtMsg = this.getWebProperties("web", "cmi_single_type.remove.check.single");
					break;
				}
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("uuid", "customer", " uuid IN ("+data+")", "0", null));
				
				//执行删除
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, args);
				break;
			case AD_LOC:
				hasDone = true;
				vct = "0";

				//TODO 验证是否存在广告
				sql = service.getSql("sql", "cmi_ad.ad_list.remove.check.ad");
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("adLocId", "customer", " adLocId IN ("+data+")", "0", null));
				
				vct = (String) service.queryForObject(sql, String.class,  beans, args);
				if(!"0".equals(vct) && vct != null){
					rtMsg = this.getWebProperties("web", "cmi_ad.ad_loc.remove.check.ad");
					break;
				}
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("uuid", "customer", " uuid IN ("+data+")", "0", null));
				
				//执行删除
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, args);
				break;
			case AD_TYPE:
				hasDone = true;
				vct = "0";

				//TODO 验证是否存在广告
				sql = service.getSql("sql", "cmi_ad.ad_list.remove.check.ad");
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("adType", "customer", " adType IN ("+data+")", "0", null));
				
				vct = (String) service.queryForObject(sql, String.class,  beans, args);
				if(!"0".equals(vct) && vct != null){
					rtMsg = this.getWebProperties("web", "cmi_ad.ad_type.remove.check.ad");
					break;
				}
				beans = new ArrayList<ConditionBean>();
				beans.add(new ConditionBean("uuid", "customer", " uuid IN ("+data+")", "0", null));
				
				//执行删除
				sql = commons.getEnumSql(e, status);
				service.executeSQL(sql, beans, args);
				break;
			default:
				errorCode = 99;
				rtMsg = this.getWebProperties("web", "msg.fail.undefiend.status");
				break;
			}
			if(!hasDone) {
				service.executeSQL(sql, beans, args);
			}
			try{
				if(log) {
					commons.noteAdminLog(doWhatMsg, user.getUuid());
				}
			} catch (Exception ex) {
			}
		} catch (Exception e) {
			errorCode = 100;
			rtMsg = this.getWebProperties("web", "msg.fail");
		}
		success.accumulate("error", errorCode);
		success.accumulate("msg", rtMsg);
		return success.toString();
	}
}
